from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from project.forms import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate
from datetime import datetime
from django.http import Http404


def index2 (request):
	return HttpResponseRedirect('/')
def index (request):
        tipo = "" 
	if request.user.is_authenticated():
       		for i in TSecretario.objects.all():
                	if i.alias == request.user:
                        	tipo="secretario"
        	if tipo == "":
                	for i in TAlumno.objects.all():
                        	if i.alias == request.user:
                                	tipo="alumno"
                	if tipo == "":
	                        for i in TProfesor.objects.all():
        	                        if i.alias == request.user:
                	                        tipo="profesor"
		persona = request.user.username
		loguearse = "True"
	else:
		loguearse = "patatafrita"
		persona =""
	
	return render_to_response("project/index.html",{'loguearse':loguearse,'persona':persona,'tipo':tipo})

@login_required
def estado (request):
        tipo = ""
        if request.user.is_authenticated():
                for i in TSecretario.objects.all():
                        if i.alias == request.user:
                                tipo="secretario"
                if tipo == "":
                        for i in TAlumno.objects.all():
                                if i.alias == request.user:
                                        tipo="alumno"
                        if tipo == "":
                                for i in TProfesor.objects.all():
                                        if i.alias == request.user:
                                                tipo="profesor"
        if tipo == "secretario":
                return HttpResponseRedirect('/')
	loguearse = "True"
	persona = request.user.username
	try:
		l = Peticion.objects.filter(alias=User.objects.get(id=request.user.id))
		l = l.order_by('fecha_comienzo')
	except:
        	raise Http500
	return render_to_response("project/estado.html",{"lista":l,'loguearse':loguearse,'persona':persona,'tipo':tipo})

@csrf_exempt
@login_required
def validar(request):
        tipo = ""
        if request.user.is_authenticated():
                for i in TSecretario.objects.all():
                        if i.alias == request.user:
                                tipo="secretario"
                if tipo == "":
                        for i in TAlumno.objects.all():
                                if i.alias == request.user:
                                        tipo="alumno"
                        if tipo == "":
                                for i in TProfesor.objects.all():
                                        if i.alias == request.user:
                                                tipo="profesor"

	loguearse = "True"
	persona = request.user.username
	if tipo == "secretario":
		secretario=True
	else:
		return HttpResponseRedirect('/')
        if request.method == 'POST':        
       		try:
			request.POST['Enviar']
                	ids=request.POST['option1']
               	 	p=Peticion.objects.get(id=ids)
                	p.estado=request.POST['accion']
                	p.save()
			if p.estado == "2":
				r=Reserva()
				r.copia(p)
				r.save()
        	except:
	        	raise Http500
	p=Peticion.objects.filter(estado="0").order_by('-fecha_comienzo')
        return render_to_response('angel/validar.html',{'lista': p,'secretario':secretario,'loguearse':loguearse,'persona':persona,'tipo':tipo})

@csrf_exempt
@login_required
def peticion_facultades(request):
        tipo = ""
        if request.user.is_authenticated():
                for i in TSecretario.objects.all():
                        if i.alias == request.user:
                                tipo="secretario"
                if tipo == "":
                        for i in TAlumno.objects.all():
                                if i.alias == request.user:
                                        tipo="alumno"
                        if tipo == "":
                                for i in TProfesor.objects.all():
                                        if i.alias == request.user:
                                                tipo="profesor"

        loguearse = "True"
        persona = request.user.username	
        if tipo == "secretario":
                return HttpResponseRedirect('/')
	uni = Universidad.objects.all()[0]
	facultades = Facultad.objects.filter(universidad=uni)
	return render_to_response('victor/PeticionPaso1.html', {'facultades':facultades,'loguearse':loguearse,'persona':persona,'tipo':tipo})

@csrf_exempt
@login_required
def peticion_plantas(request, facul):
        tipo = ""
        if request.user.is_authenticated():
                for i in TSecretario.objects.all():
                        if i.alias == request.user:
                                tipo="secretario"
                if tipo == "":
                        for i in TAlumno.objects.all():
                                if i.alias == request.user:
                                        tipo="alumno"
                        if tipo == "":
                                for i in TProfesor.objects.all():
                                        if i.alias == request.user:
                                                tipo="profesor"

        loguearse = "True"
        persona = request.user.username
        if tipo == "secretario":
                return HttpResponseRedirect('/')
	plantas = Planta.objects.filter(facultad__pk=facul)
	return render_to_response('victor/PeticionPaso2.html', {'plantas':plantas,'loguearse':loguearse,'persona':persona,'tipo':tipo})

@csrf_exempt
@login_required
def peticion_aulas(request, plan):
        tipo = ""
        if request.user.is_authenticated():
                for i in TSecretario.objects.all():
                        if i.alias == request.user:
                                tipo="secretario"
                if tipo == "":
                        for i in TAlumno.objects.all():
                                if i.alias == request.user:
                                        tipo="alumno"
                        if tipo == "":
                                for i in TProfesor.objects.all():
                                        if i.alias == request.user:
                                                tipo="profesor"

        loguearse = "True"
        persona = request.user.username
        if tipo == "secretario":
                return HttpResponseRedirect('/')
	aulas = Aula.objects.filter(planta__pk=plan)
	horas = [7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
	return render_to_response('victor/PeticionPaso3.html', {'aulas':aulas, 'horas':horas,'loguearse':loguearse,'persona':persona,'tipo':tipo})

@csrf_exempt
@login_required
def peticion_final(request):
        tipo = ""
        if request.user.is_authenticated():
                for i in TSecretario.objects.all():
                        if i.alias == request.user:
                                tipo="secretario"
                if tipo == "":
                        for i in TAlumno.objects.all():
                                if i.alias == request.user:
                                        tipo="alumno"
                        if tipo == "":
                                for i in TProfesor.objects.all():
                                        if i.alias == request.user:
                                                tipo="profesor"
        loguearse = "True"
        persona = request.user.username
        if tipo == "secretario":
                return HttpResponseRedirect('/')

	id_aula = request.POST['aula']
	aula_pet = Aula.objects.filter(id=int(id_aula))[0]
	fecha = datetime(int(request.POST['anyo']), int(request.POST['mes']), int(request.POST['dia']), int(request.POST['hora']))
	concep = request.POST['concep'] 

	peticion = Peticion(alias=request.user, aula=aula_pet, fecha_comienzo=fecha  ,estado="0", concepto = concep)
	peticion.save()
	return render_to_response('project/completado.html',{'tipo':tipo,'loguearse':loguearse,'persona':persona})
